﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace KiDS_Bootstrap
{
	/// <summary>
	/// 
	/// </summary>
	public class Pinger
	{
		private readonly Timer _timer;
		private readonly List<Servent> _servents = new List<Servent>();
		private readonly System.Threading.Thread _thread;
		private static Pinger _pinger = new Pinger(Properties.Settings.Default.PingInterval);

		public static Pinger Instance
		{
			get
			{
				return _pinger;
			}
		}


		public Pinger(int interval)
		{
			_timer = new Timer(interval);
			_thread = new System.Threading.Thread(() =>
			{
				_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
				_timer.Start();
			});
			_thread.Start();
		}

		public void Subscribe(Servent servent)
		{
			_servents.Add(servent);
		}

		public void Unsubscribe(Servent servent)
		{
			_servents.Remove(servent);
		}


		public bool Contains(Servent servent)
		{
			foreach(var s in _servents)
				if(s == servent)
					return true;
			return false;
		}

		private void OnTimedEvent(object source, ElapsedEventArgs e)
		{
			for(int i = 0; i  < _servents.Count; i++)
			{
				//_servents[i]
			}
		}

		public int IndexOf(string location)
		{
			for (int i = 0; i < _servents.Count; i++)
			{
				if (_servents[i].Location == location)
					return i;
			}
			return -1;
		}

		public void Remove(int index)
		{
			_servents.RemoveAt(index);
		}
	}
}
